package com.itttq.gen.model.to;


import java.io.Serializable;

/**
 * 返回给页面展示的对象，此处只返回消息
 */
public class ResultTo<T> implements Serializable {
    private static final long serialVersionUID = 606324890719724796L;

    public enum ResultCode {
        /**
         * 默认成功
         */
        SUCCESS(1, "成功"),
        // 默认失败
        FAIL(2, "操作失败，请稍后重试"),
        // 验证码错误
        CODE_ERR(3, "验证码错误"),
        // 传入参数不正确，包括未传完整参数，传入参数格式不对等
        PARAME_ERR(4, "传入参数有误"),
        // 无数据
        DATA_NO(5, "没有查询到数据"),
        // 数据存在，如用户存在等
        DATA_EXIST(6, "数据已存在"),
        // ID不能为空
        ID_NULL(7, "ID不能为空"),
        // 状态不对
        STATUS_ERR(8, "当前状态不允许做此操作"),
        // 校验失败
        VERIFY_TOKEN_FAIL(9, "Token校验不通过"),
        // 校验失败
        VERIFY_IP_FAIL(10, "IP校验不通过"),
        // 校验用户名或密码失败
        VERIFY_USER_FAIL(11, "用户名或密码错误"),
        // 解析异常：包括Json解析、其他数据格式解析等都用这个
        PARSE_ERR(12, "数据解析异常"),
        // 解析异常：包括Json解析、其他数据格式解析等都用这个
        AUTH_FAIL(13, "您无权做此操作"),
        // 只能一个操作
        UNIQUE_ERR(14, "不允许多人同时操作，请刷新数据后重试"),
        // SESSION失效
        SESSION_INVALID(9999, "您的登录凭证已过期，请重新登录"),

        /**
         * 账号相关：30~50
         */
        ACC_USER_EXIST(30, "用户名已经存在"),
        ACC_USER_NO(31, "用户名不存在"),
        ACC_LOGIN_FAIL(32, "用户名或密码错误"),
        ACC_USER_DISABLE(33, "用户已被禁用")
        ;

        private int code;
        private String msg;

        ResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    /**
     * 错误代码。
     */
    private int code;
    private String msg;
    private T data;

    public T getData() {
        return data;
    }

    public ResultTo<T> setData(T data) {
        this.data = data;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * 默认成功
     */
    public ResultTo() {
    }

    public ResultTo(ResultCode code) {
        this.code = code.getCode();
        this.msg = code.getMsg();
    }

    public ResultTo(ResultCode code, String msg) {
        this.code = code.getCode();
        this.msg = msg;
    }

    public ResultTo(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResultTo success() {
        return new ResultTo(ResultCode.SUCCESS);
    }

    public static ResultTo success(String msg) {
        return new ResultTo(ResultCode.SUCCESS, msg);
    }

    public static ResultTo success(Object data) {
        return new ResultTo(ResultCode.SUCCESS).setData(data);
    }

    public static ResultTo fail() {
        return new ResultTo(ResultCode.FAIL);
    }

    public static ResultTo fail(String msg) {
        return new ResultTo(ResultCode.FAIL, msg);
    }

    public static ResultTo fail(ResultCode resultCode) {
        return new ResultTo(resultCode, resultCode.getMsg());
    }

    public static ResultTo fail(ResultCode resultCode, String msg) {
        return new ResultTo(resultCode, msg);
    }

    public static ResultTo fail(int code, String msg) {
        return new ResultTo(code, msg);
    }

    public static boolean isSuccess(ResultTo result) {
        return null != result && result.code == ResultCode.SUCCESS.getCode();
    }

    public static String getCodeAndMsg(ResultCode code, String msg) {
        return code.getCode() + "-" + msg;
    }

    public static String getCodeAndMsg(ResultCode code) {
        return code.getCode() + "-" + code.msg;
    }
}
